package com.example.ipcdemo.service;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.NonNull;

public class MessengerService extends Service {
    private static final String TAG = "MessageService";
    public MessengerService() {
    }



    Messenger mMessenger = new Messenger(new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            try {
                int num = msg.arg1 + msg.arg2;
                Message message = Message.obtain();
                message.what = 2;
                message.arg1 = num;
                //通过客户端的 Messenger 来把数据发送回去
                msg.replyTo.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    });

    @Override
    public IBinder onBind(Intent intent) {
        return mMessenger.getBinder();
    }
}
